
package com.gugalor.adapter;

import android.content.Context;
import android.graphics.BitmapFactory.Options;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.jianzhile.jobs.R;
import com.jianzhile.jobs.utils.CommonUtil;

import java.util.ArrayList;

/**
 * 相册adapter
 */
public class AlbumGridViewAdapter extends BaseAdapter implements OnClickListener {

    private static final String TAG = "AlbumGridViewAdapter";
    private Context mContext;
    // 相册数据 list
    private ArrayList<String> mDataList;
    // 被选中的相册数据list
    private ArrayList<String> mSelectedDataList;
    private LayoutInflater mInflater;
    private Options mDecodingOptions;
    // private DDApplication mApp;
    private int width = 0;

    public AlbumGridViewAdapter(Context mContext, ArrayList<String> mDataList, ArrayList<String> mSelectedDataList) {
        super();
        this.mContext = mContext;
        this.mDataList = mDataList;
        this.mSelectedDataList = mSelectedDataList;

        // mApp = (DDApplication) mContext.getApplicationContext();
        width = CommonUtil.getDisplayWidth(mContext);
        mDecodingOptions = new Options();
        mDecodingOptions.outWidth = width / 4;

        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return (null == mDataList) ? 0 : mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return (null == mDataList) ? null : mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == mContext) {
            return null;
        }
        if (null == mDataList || mDataList.isEmpty() || mDataList.size() <= position) {
            return null;
        }
        final ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.album_grid_view_item, parent, false);

            holder.photoImg = (ImageView) convertView.findViewById(R.id.photo_img);

            holder.photoImg.setMaxWidth(width / 4);
            holder.photoImg.setMaxHeight(width / 4);
            holder.photoImg.setLayoutParams(new RelativeLayout.LayoutParams(width / 4, width / 4));

            holder.selectChk = (CheckBox) convertView.findViewById(R.id.select_chk);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String path = mDataList.get(position);
        if (TextUtils.isEmpty(path)) {
            path = "";
        }
      //  mImageLoader.displayImage("file://" + path, holder.photoImg, mOptions);
        Glide.with(mContext).load("file://" + path).into(holder.photoImg);
        // 为checkbox设置tag
        holder.selectChk.setTag(position);
        holder.selectChk.setOnClickListener(this);
        holder.selectChk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //doAnimation(holder.selectChk);
                }
            }
        });
        if (isInSelectedDataList(path)) {
            holder.selectChk.setChecked(true);
        } else {
            holder.selectChk.setChecked(false);
        }

        return convertView;
    }

    private class ViewHolder {
        ImageView photoImg;
        CheckBox selectChk;
    }

    @Override
    public void onClick(View view) {
        if (view instanceof CheckBox) {
            CheckBox toggleButton = (CheckBox) view;
            int position = (Integer) toggleButton.getTag();
            if (null != mDataList && mOnItemClickListener != null && position < mDataList.size()) {
                mOnItemClickListener.onItemClick(toggleButton, position, mDataList.get(position), toggleButton.isChecked());
            }
        }
    }

    // 点击回调
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        public void onItemClick(CheckBox toggleButton, int position, String path, boolean isChecked);
    }

    /**
     * 判断是否在已选中
     */
    private boolean isInSelectedDataList(String selectedString) {
        if (null != mSelectedDataList) {
            for (int i = 0; i < mSelectedDataList.size(); i++) {
                if (mSelectedDataList.get(i).equals(selectedString)) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * checkbox 选中时，做动画
     */
    private void doAnimation(View view) {
        ScaleAnimation animation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f, ScaleAnimation.RELATIVE_TO_SELF, 1f,
                ScaleAnimation.RELATIVE_TO_SELF, 1f);
        animation.setDuration(150);
        view.startAnimation(animation);
    }

}
